﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE;

namespace UI
{
    public partial class Checkout : System.Web.UI.Page
    {
        string[] forms = new string[] { "First Name", "Last Name", "Steet (Name & Number)", "City", "Country", "Email" };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ShoppingCart.Instance.Cart.Count < 1)
            {
                Label l = new Label();
                l.Text = "You have not added any albums to your cart yet, please choose some items before checking out.<br /> you will be redirected in 5 seconds <meta http-equiv=\"refresh\" content=\"5;url=MusicPage.aspx\" /> ";
                ContentPlaceHolder ctp = Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
                ctp.Controls.Add(l);
                Wizard1.Visible = false;
            }
            else
            {
                createCheckoutTable();
            }
        }

        private void createCheckoutTable()
        {
            foreach (string form in forms)
            {
                TableRow newRow = new TableRow();
                tblCheckout.Controls.Add(newRow);

                Label lbl = new Label();
                TextBox txtBox = new TextBox();

                lbl.Text = form;
                txtBox.ID = form;

                newRow.Controls.Add(createCell(lbl));
                newRow.Controls.Add(createCell(txtBox));
            }
        }

        private void createEndFormTable2()
        {
            foreach (Album album in ShoppingCart.Instance.Cart)
            {
                TableRow newRow = new TableRow();
                tblEnd2.Controls.Add(newRow);

                Label lblItem = new Label();
                Label lblPrice = new Label();

                lblItem.Text = album.Artist + " - " + album.Title;
                lblPrice.Text = album.DecimalOutPut;

                newRow.Controls.Add(createCell(lblItem));
                newRow.Controls.Add(createCell(lblPrice));
            }
        }

        private void createEndFormTable1()
        {
            
            foreach (string form in forms)
            {
                TableRow newRow = new TableRow();
                tblEnd1.Controls.Add(newRow);

                Label lblForm = new Label();
                Label lblEntered = new Label();

                lblForm.Text = form + ": ";
                string input = Server.HtmlEncode(((TextBox)Wizard1.FindControl(form)).Text);
                if (input.Equals(""))
                {
                    Response.Redirect("Checkout.aspx");
                    break;
                }
                lblEntered.Text = input;

                newRow.Controls.Add(createCell(lblForm));
                newRow.Controls.Add(createCell(lblEntered));
            }
        }

        private TableCell createCell(Control ctrl)
        {
            TableCell cell = new TableCell();
            cell.Controls.Add(ctrl);
            return cell;
        }

        protected void OnNextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            createEndFormTable1();
            createEndFormTable2();
            Master.UpdateCart();
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            ShoppingCart.Instance.ClearCart();
            Master.UpdateCart();
            Response.Redirect("OrderComplete.aspx");
        }
    }
}